package com.wzdigit.wms.wms.service.transfer;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.SearchGdnHeaderReportRequest;
import com.wzdigit.wms.basic.client.response.transfer.GdnDtlGoodsReportResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnDtlReportResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnHeaderAndDtlReportResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnHeaderReportResponse;
import com.wzdigit.wms.basic.enums.GdnDtlStatusEnum;
import com.wzdigit.wms.basic.enums.GdnTypeEnum;
import com.wzdigit.wms.basic.enums.TrsnNoteTypeEnum;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.GdnHeaderMapper;
import com.wzdigit.wms.basic.util.DateUtils;
import com.wzdigit.wms.basic.util.enums.SystemModuleEnum;
import com.wzdigit.wms.common.utils.EasyExcelUtil;
import com.wzdigit.wms.common.utils.HttpExcel;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 出库报表-服务类
 * 
 * @author yangdongjie
 * @GdnService.java
 * @2021年2月24日下午3:25:03
 */
@Service
public class GdnReportService {

	@Autowired
	private GdnHeaderMapper gdnHeaderMapper;

	@Autowired
	private GdnDtlMapper gdnDtlMapper;

	@Autowired
	private GdnDtlGoodsMapper gdnDtlGoodsMapper;
	
	private static final String OUT_STORAGE_CODE = TrsnNoteTypeEnum.OUT_STORAGE.getCode();
	
	public static List<String> gdnTypeList=GdnTypeEnum.getCodeListBySystemModule(SystemModuleEnum.WMS);	// 发货通知单类型

	/**
	 * 出库报表-主档查询
	 * 
	 * @param dto
	 * @param pager
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public <T> PagerInfo<List<GdnHeaderReportResponse>> selectGdnHeaderReportList(SearchGdnHeaderReportRequest dto,
			Pager<T> pager) {
		Page page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
		Date beginDate = null, endDate = null;Date closeBeginTime =null;Date closeEndTime =null;
		if (dto.getBeginTime() != null) {
			beginDate=new Date(dto.getBeginTime());
			//beginDate = DateUtils.getStartTime(dto.getBeginTime());
		}
		if (dto.getEndTime() != null) {
			endDate=new Date(dto.getEndTime());
			//endDate = DateUtils.getEndTime(dto.getEndTime());
			/*Calendar calendar = new GregorianCalendar();
			calendar.setTime(endDate);
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
			endDate=calendar.getTime();*/
		}
		if (dto.getCloseBeginTime() != null) {
			closeBeginTime=new Date(dto.getCloseBeginTime());
			//closeBeginTime = DateUtils.getStartTime(dto.getCloseBeginTime());
		}
		if (dto.getCloseEndTime() != null) {
			closeEndTime=new Date(dto.getCloseEndTime());
			//closeEndTime = DateUtils.getEndTime(dto.getCloseEndTime());
			/*Calendar calendar = new GregorianCalendar();
			calendar.setTime(closeEndTime);
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
			closeEndTime=calendar.getTime();*/
		}
		PagerInfo<List<GdnHeaderReportResponse>> pagerInfo = new PagerInfo<>();
		List<GdnHeaderReportResponse> list = gdnHeaderMapper.
				selectGdnHeaderReportList(dto, gdnTypeList, beginDate, endDate,
						closeBeginTime,closeEndTime, GdnDtlStatusEnum.CLOSE.code);
		pagerInfo.setItems(list);
		pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
	}

	/**
	 * 出库报表-明细查询
	 * 
	 * @param gdnHeaderId
	 * @return
	 */
	public List<GdnDtlReportResponse> selectGdnDtlReportList(Integer gdnHeaderId,Long beginDate,Long endDate) {
		Date closeBegin =null;
		Date closeEnd =null;
		if (beginDate!=null){
			closeBegin=new Date(beginDate);
		}
		if (endDate!=null){
			closeEnd=new Date(endDate);
			/*Calendar calendar = new GregorianCalendar();
			calendar.setTime(closeEnd);
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
			closeEnd=calendar.getTime();*/
		}
		List<GdnDtlReportResponse> list = gdnDtlMapper.selectGdnDtlReportList(gdnHeaderId,closeBegin,closeEnd);
		for (GdnDtlReportResponse dtl : list) {
			if (dtl!=null){
				if (dtl.getGdnDtlStatus()==null){
					dtl.setCloseTime(null);
				}
				else if (!dtl.getGdnDtlStatus().equals(GdnDtlStatusEnum.CLOSE.code)&&!dtl.getGdnDtlStatus()
						.equals(GdnDtlStatusEnum.SHIPMENTED.code)){
					dtl.setCloseTime(null);
				}
			}
		}
		return list;
	}

	/**
	 * 出库报表-明细货品清单查询
	 * 
	 * @param
	 * @param pager
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public <T> PagerInfo<List<GdnDtlGoodsReportResponse>> selectGdnDtlGoodsReportList(Long gdnHeaderId, Pager<T> pager) {
		Page page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
		PagerInfo<List<GdnDtlGoodsReportResponse>> pagerInfo = new PagerInfo<>();
		List<GdnDtlGoodsReportResponse> list = gdnDtlGoodsMapper.selectGdnDtlGoodsReportList(gdnHeaderId, OUT_STORAGE_CODE);
		pagerInfo.setItems(list);
		pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
	}

	/**
	 * 导出出库报表-明细货品清单
	 * 
	 * @param gdnHeaderId
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void exportGdnDtlGoodsReportList(Long gdnHeaderId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<GdnDtlGoodsReportResponse> dataSet = gdnDtlGoodsMapper.selectGdnDtlGoodsReportList(gdnHeaderId, OUT_STORAGE_CODE);
		
		ExportParams params = new ExportParams();
		params.setType(ExcelType.XSSF);
		params.setHeight((short)5);
		Workbook workbook = ExcelExportUtil.exportExcel(params, GdnDtlGoodsReportResponse.class, dataSet);
		
		String fileName = "出库报表-明细货品清单";
		HttpExcel.downLoadExcel(fileName, workbook, request, response);
	}

	/**
	 * 导出出库报表-主档和明细
	 * @param
	 * 
	 * @param
	 * @param request
	 * @param response
	 * @return 
	 * @throws Exception
	 */
	public void exportGdnGdnHeaderAndDtlReportList(
			SearchGdnHeaderReportRequest dto, 
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Date beginDate = null, endDate = null;
		Date closeBeginTime = null;
		Date closeEndTime = null;
		if (dto.getBeginTime() != null) {
			beginDate = DateUtils.getStartTime(dto.getBeginTime());
		}
		if (dto.getEndTime() != null) {
			endDate = DateUtils.getEndTime(dto.getEndTime());
			/*Calendar calendar = new GregorianCalendar();
			calendar.setTime(endDate);
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
			endDate=calendar.getTime();*/
		}
		if (dto.getCloseBeginTime() != null) {
			closeBeginTime = DateUtils.getStartTime(dto.getCloseBeginTime());
		}
		if (dto.getCloseEndTime() != null) {
			closeEndTime = DateUtils.getEndTime(dto.getCloseEndTime());
			/*Calendar calendar = new GregorianCalendar();
			calendar.setTime(closeEndTime);
			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
			closeEndTime=calendar.getTime();*/
		}

		List<GdnHeaderAndDtlReportResponse> dataSet = gdnDtlMapper.selectGdnHeaderAndDtlReportList(dto, gdnTypeList,
				beginDate, endDate, closeBeginTime, closeEndTime, GdnDtlStatusEnum.CLOSE.code);

		/*for (GdnHeaderAndDtlReportResponse dtl : dataSet) {
			if (dtl.getGdnDtlStatus()==null){
				dtl.setCloseTime(null);
			}
			else if (!dtl.getGdnDtlStatus().equals(GdnDtlStatusEnum.CLOSE.code)&&!dtl.getGdnDtlStatus()
					.equals(GdnDtlStatusEnum.SHIPMENTED.code)){
				dtl.setCloseTime(null);
			}
		}*/
		
		EasyExcelUtil.setResponse(request, response, "出库报表-主档和明细");
		EasyExcelUtil.export(response, GdnHeaderAndDtlReportResponse.class, dataSet, "1");
	}
	
	/*public void exportGdnGdnHeaderAndDtlReportList(SearchGdnHeaderReportRequest dto, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Date beginDate = null, endDate = null;
		Date closeBeginTime = null;
		Date closeEndTime = null;
		if (dto.getBeginTime() != null) {
			beginDate = DateUtils.getStartTime(dto.getBeginTime());
		}
		if (dto.getEndTime() != null) {
			endDate = DateUtils.getEndTime(dto.getEndTime());
		}
		if (dto.getCloseBeginTime() != null) {
			closeBeginTime = DateUtils.getStartTime(dto.getCloseBeginTime());
		}
		if (dto.getCloseEndTime() != null) {
			closeEndTime = DateUtils.getEndTime(dto.getCloseEndTime());
		}
		List<GdnHeaderAndDtlReportResponse> dataSet = gdnDtlMapper.selectGdnHeaderAndDtlReportList(dto, gdnTypeList,
				beginDate, endDate, closeBeginTime, closeEndTime, GdnDtlStatusEnum.CLOSE.code);

		// 需要合并的列
		int[] mergeColumeIndex = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };

		// 从那一行开始合并
		int mergeRowIndex = 1;

		// 先执行合并策略
		ExcelWriter excelWriter = ExcelMergeEncodingUtils.getExcelWriterMerge(response, "出库报表-主档和明细", mergeRowIndex,mergeColumeIndex);
		// 给定导出实体类
		WriteSheet sheetWriter = EasyExcel.writerSheet().head(GdnHeaderReportResponse.class).build();
		excelWriter.write(dataSet, sheetWriter);
	}*/
}
